<template>
  <el-row :gutter="24" class="el-row">
    <el-col :span="24" class="el-card" align="center">
      <div class="grid-content bg-purple">

        <el-row :gutter="24" class="el-row">
          <el-form :inline="true" class="demo-form-inline">
            <el-form-item label="优惠券时间">
              <el-date-picker v-model="startDate" type="date" placeholder="选择日期" @change="dataSearch"
                              value-format="yyyy-MM-dd"></el-date-picker>
            </el-form-item>
            <el-form-item label="优惠券名称">
              <el-select v-model="query.couponName" placeholder="请选择" class="dialog-input">
                <el-option v-for="(item,index) in couponNameOptions" :key="item.index" :label="item.couponName"
                           :value="item.couponName"/>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="handleFilter">查询</el-button>
            </el-form-item>
          </el-form>
        </el-row>
        <!--由listLoading决定是否加载-->
        <el-table stripe :data="list" v-loading="listLoading" element-loading-text="Loading..."
                  border fit highlight-current-row
                  style="width: 100%;margin:20px 0" align="center"
                  :header-cell-style="{background:'#eef1f6',color:'#606266'}">
          <el-table-column
            prop="couponId"
            label="优惠券ID"
            width="120">
          </el-table-column>
          <el-table-column
            prop="couponName"
            label="优惠券名称"
            width="230">
          </el-table-column>
          <el-table-column
            prop="startDate"
            label="活动开始时间">
          </el-table-column>
          <el-table-column
            prop="ruleName"
            label="优惠规则" width="500">
          </el-table-column>
          <el-table-column
            prop="reduceRate"
            label="补贴率(%)" width="120" :formatter="formatRate">
          </el-table-column>
        </el-table>

        <!-- 分页组件 -->
        <el-pagination
          :current-page="page"
          :total="total"
          :page-size="limit"
          :page-sizes="[5, 10, 20, 30, 40, 50, 100]"
          style="padding: 30px 0; text-align: center;"
          layout="sizes, prev, pager, next, jumper, ->, total, slot"
          @current-change="fetchData"
          @size-change="changeSize"
        />
      </div>
    </el-col>
  </el-row>
</template>


<script>
import api from '@/api/statistics/coupon/coupon'
import {parseTime} from '@/utils'

const now = parseTime(new Date(new Date().getTime()), '{y}-{m}-{d}')
export default {
  data() {
    return {
      startDate: new Date(),
      couponName: '',
      couponNameOptions: [],
      total: 0, // 数据库中的总记录数
      page: 1, // 默认页码
      limit: 10, // 每页记录数
      query: {
        startDate: now,
        couponName: ""
      },
      list: [],
      listLoading: false
    };
  },
  created() {
    this.initData({});
  },
  methods: {
    handleFilter() {
      this.query.pageNo = 1;
      this.fetchData();
    },
    dataSearch() {
      this.query.startDate = this.startDate
      this.getCouponName(this.query)
    },
    // 当页码发生改变的时候
    changeSize(size) {
      console.log(size)
      this.limit = size
      this.fetchData(1)
    },

    // 加载banner列表数据
    fetchData(page = 1) {
      console.log('翻页。。。' + page)
      // 异步获取远程数据（ajax）
      this.page = page
      /**
       * 各渠道流量统计接口
       */
      api.getCouponList(this.query, this.page, this.limit).then(response => {
        // console.log("-----" + this.query.startDate)
        this.listLoading = true
        this.list = response.data.records
        // console.log("========tableData"+this.tableData)
        this.total = response.data.total
        this.listLoading = false

      })
    },
    async initData(data) {
      //获取当前时间
      var now = new Date();
      var monthn = now.getMonth() + 1;
      var yearn = now.getFullYear();
      var dayn = now.getDate();
      this.startDate = yearn + "-" + monthn + "-" + dayn;

      this.getCouponName();
    },
    async getCouponName(data) {
      api.getCouponNameByStartDate(this.query).then(response => {
        this.couponNameOptions = response.data;
        console.log(this.couponNameOptions)
      })
    },
    flushCurrent(p, type) {
      if (type === 'del') {
        if (p > (((this.total - 1) / this.query.pageSize)) + 1) {
          p = p - 1
        }
      } else {
        if (p > (((this.total + 1) / this.query.pageSize)) + 1) {
          p = p + 1
        } else {
          p = (this.total + 1) / this.query.pageSize + (this.total + 1) % this.query.pageSize
        }
      }
      this.query.pageNo = p
      this.getCouponList()
    },

    formatRate(row, col, value, index) {
      return parseFloat(value).toFixed(2) + "%"

    }
  }

}
</script>

